﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Common.Logging;
using User.Api.Helpers;
using Newtonsoft.Json;
using OutDbContext;

namespace User.Api
{
    /// <summary>
    /// BeingPushed 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class PushedWebService : System.Web.Services.WebService
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(PushedWebService));
        const string TopColor = "#0000FF";

        const string FirstColor = "#173177";

        const string RemarkColor = "#173177";

        const string NoticeColor = "#173177";
        /// <summary>
        /// 取消成功通知
        /// </summary>
        /// <param name="FirstData"></param>
        /// <param name="FirstData"></param>
        /// <param name=""></param>
        /// <returns></returns>
        [WebMethod]
        public string PushedAppt(string FirstData, string QueueNo, string MethodID, string PatientName, string PatientSex, string PatientTel, string PatientIdCard, string ReservationIdentity, string StudyKindName, string CheckSite, string ReserveBeginTime, string HospitalName, string DepartmentPlace, string PatientHisID, string ReserveEndTime, string StudyNotice, string ReserveNotice, int OpenId)
        {
            #region 发送模板消息

            var templateId = "gOfnsWQ2KBX9YKBTrpww0_Hd9nuZkFPVk3ZuvTI0pOM";
            var strremark = $"";
            if (MethodID == "YY1001")
            {
                strremark = FirstData;
            }
            else if (MethodID == "")
            {

            }
            var url = MessageHelper.GetH5Url("/index/get-queue");
            var data = new
            {
                first = new
                {
                    value = strremark,
                    color = FirstColor
                },
                keyword1 = new
                {
                    value = PatientName, //就诊人
                    color = NoticeColor
                },
                keyword2 = new
                {
                    value = PatientIdCard, //卡号
                    color = NoticeColor
                },
                keyword3 = new
                {
                    value = HospitalName, //就诊医院
                    color = NoticeColor
                },
                keyword4 = new
                {
                    value = DepartmentPlace, //就诊科室
                    color = NoticeColor
                },
                keyword5 = new
                {
                    value = ReserveBeginTime + " " + ReserveEndTime, //就诊时间
                    color = NoticeColor
                },
                remark = new
                {
                    value = "请您带齐相关资料，于就诊前到15分钟到达我院候诊，谢谢合作",
                    color = RemarkColor
                }
            };
            using (var ctx = new FsssdqfybjyEntities())
            {
                var pushed = ctx.BeingPusheds.FirstOrDefault(x => x.Id == OpenId);
                pushed.YjStatus = 1;
                pushed.UpdateTime = DateTime.Now;
                pushed.Action = FirstData;
                ctx.SaveChanges();
            }
            try
            {
                MessageHelper.SendTemplateMessage("预约就诊提醒通知", strremark, null, "", templateId, TopColor,
                    url, data);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("发送预约就诊提醒消息时出错，信息：{0}", JsonConvert.SerializeObject(ex));
            }
            #endregion
            return "HelloWorld！";
        }

    }
}
